class UniqueArray {
  private array: number[];
  private memberRecorded: boolean[];
  private maxSize: number;

  constructor(size = 81) {
    this.array = [];
    this.memberRecorded = new Array(size + 1).fill(false);
    this.maxSize = size;
  }

  public push(num: number): void {
    if (this.memberRecorded[num]) {
      return;
    }
    if (num > this.maxSize || num < 0) {
      throw new Error(`invalid number: ${num}, must between 0~${this.maxSize}`);
    }
    this.array.push(num);
    this.memberRecorded[num] = true;
  }

  public pop(): number {
    const num = this.array.pop();
    if (num === undefined) {
      return undefined;
    }
    this.memberRecorded[num] = false;
    return num;
  }

  public get length(): number {
    return this.array.length;
  }

  public has(num: number): boolean {
    return this.memberRecorded[num];
  }

  public isEmpty(): boolean {
    return this.array.length === 0;
  }

  public get data(): number[] {
    return this.array;
  }
}

export default UniqueArray;
